//
//  HDNewFeatureViewController.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/11.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HDNewFeatureViewController: UICollectionViewController {
   //布局参数
    let layout = UICollectionViewFlowLayout()
     init() {
        super.init(collectionViewLayout :layout)
        //设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
         layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(HDNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }



    // MARK: 数据源方法


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return NewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!HDNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
}
//代理方法
extension HDNewFeatureViewController {
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算页面索引
        let index = Int( scrollView.contentOffset.x/ScreenW)
        //判断是否是最后一页 调用按钮的方法
        if index == 3 {
            //获取最后一个cell  来调用方法
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HDNewFeatureCell
            cell.startButton()
        }
    }
}


class HDNewFeatureCell : UICollectionViewCell {
    
    //MARK -初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //最后一个界面 显示按钮  用UIView 做tranform动画
    func startButton () {
        startBtn.hidden = false
        //设置tranform为0  按钮就没有了
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        //动画清除 tranform 还原了
        UIView.animateWithDuration(animationCurtation, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                HDPrint("xxxxx")
        }
        
    }
    
    //添加子控件和设置约束
    private func prepareUI () {
        contentView.addSubview(bgImageView)
        contentView.addSubview(startBtn)
        //背景图片约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        //按钮约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
        
    }
    
    //用属性观察期来给子控件赋值
    var index : Int = 0 {
        didSet {
            //拼接图片
            let imageName =  "new_feature_\(index + 1)"
            //设置背景图片
            bgImageView.image = UIImage(named: imageName)
            //加载图片时候按钮就隐藏  避免cell重用的问题
            startBtn.hidden = true
        }
    }
    
    //MARK - 懒加载控件
    //背景图片
    private lazy var bgImageView:UIImageView =  UIImageView()
    //按钮
    private lazy var startBtn :UIButton  = {
        let btn = UIButton()
        //设置按钮的属性
       btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState:UIControlState.Highlighted)
        btn.sizeToFit()
        btn.hidden = true
        //绑定监听事件
        btn.addTarget(self, action: Selector("startBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //实现按钮监听方法
    @objc private  func startBtnClick() {
        (UIApplication.sharedApplication().delegate as! AppDelegate ).switchViewController(true)
    }
    
}
